package com.sise.controller;

import com.sise.bean.CodeMsg;
import com.sise.entity.ProductType;
import com.sise.service.IProductTypeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zj
 */
@RestController
@RequestMapping("/sise/product-type")
public class ProductTypeController {

    @Resource
    private IProductTypeService productTypeService;

    @RequestMapping("/findByStoreId")
    public List<ProductType> findByStoreId(Integer id) {
        return productTypeService.findByStoreId(id);
    }

    @PostMapping("/deleteById")
    public CodeMsg deleteById(Integer id) {
        boolean isDelete;
        try {
            isDelete = productTypeService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new CodeMsg().setCode(401).setMsg("改商品类型存在商品，应该把商品删除后再删除商品类型！");
        }
        if (isDelete) {
            return new CodeMsg().setCode(200).setMsg("删除成功！");
        }
        return new CodeMsg().setCode(400).setMsg("删除失败！");
    }

    @PostMapping("/updateById")
    public CodeMsg updateById(ProductType productType) {
        boolean isUpdate = productTypeService.updateById(productType);
        if (isUpdate) {
            return new CodeMsg().setCode(200).setMsg("修改成功！");
        }
        return new CodeMsg().setCode(400).setMsg("修改失败！");
    }

    @PostMapping("/insertProductType")
    public CodeMsg save(ProductType productType) {
        boolean isSave = productTypeService.save(productType);
        if (isSave) {
            return new CodeMsg().setCode(200).setMsg("添加成功！");
        }
        return new CodeMsg().setCode(400).setMsg("添加失败！");
    }
}
